/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/11/29
*/
#include "LuaWindow.h"
#include "../edui.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

#ifdef EDUI_TEST_NEW_DELETE
int LuaWindow::m_newdeleteNum = 0;
int LuaWindow::_GetNewDeleteNum()
{
    return m_newdeleteNum;
}
#endif

void* LuaWindow::GetInterface(void* data)
{
    if (!data) return 0;
    LuaWindow* wnd = new LuaWindow((IWindow*)data);
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[LuaWindow] new LuaWindow : %d, %d", wnd, data);
#endif
    return wnd;
}

void LuaWindow::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[LuaWindow] delete LuaWindow : %d, %d, %s", this, m_cobj, this->GetName());
#endif
    delete this;
}

void* LuaWindow::GetCObj()
{
    return m_cobj;
}

LuaWindow::LuaWindow(IWindow* wnd)
{
    m_cobj = wnd;
}

LuaWindow::~LuaWindow()
{}

//----------------------------------------//

bool LuaWindow::SetName(const char* name)
{
    return m_cobj->SetName(name);
}

const char* LuaWindow::GetName()
{
    return m_cobj->GetName();
}

Uint32 LuaWindow::GetID()
{
    return m_cobj->GetID();
}

Uint32 LuaWindow::GetFlags()
{
    return m_cobj->GetFlags();
}

const char* LuaWindow::GetTitle()
{
    return m_cobj->GetTitle();
}

void LuaWindow::SetTitle(const char* title)
{
    m_cobj->SetTitle(title);
}

int LuaWindow::SetIcon(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    ISurface* surface = (ISurface*) LuaHelperNS::GetCObjectData(args[2]);
    m_cobj->SetIcon(surface);
    return 0;
}

int LuaWindow::GetIcon(LuaState *L)
{
    ISurface* surface = m_cobj->GetIcon();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, surface);
    obj.Push(L);
    return 1;
}

bool LuaWindow::SetCaptionRect(int x, int y, int w, int h)
{
    Rect rc(x, y, w, h);
    return m_cobj->SetCaptionRect(&rc);
}

int LuaWindow::GetCaptionRect(LuaState *L)
{
    Rect rc = m_cobj->GetCaptionRect();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &rc);
    obj.Push(L);
    return 1;
}

bool LuaWindow::SetSizeBox(int l, int t, int r, int b)
{
    Rect1 rc1(l, t, r, b);
    return m_cobj->SetSizeBox(&rc1);
}

int LuaWindow::GetSizeBox(LuaState *L)
{
    Rect1 rc1 = m_cobj->GetSizeBox();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &rc1);
    obj.Push(L);
    return 1;
}

int LuaWindow::GetWidth()
{
    return m_cobj->GetWidth();
}

int LuaWindow::GetHeight()
{
    return m_cobj->GetHeight();
}

int LuaWindow::GetPos(LuaState *L)
{
    Point pt = m_cobj->GetPos();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &pt);
    obj.Push(L);
    return 1;
}

void LuaWindow::SetPos(int x, int y)
{
    m_cobj->SetPos(x, y);
}

int LuaWindow::GetSize(LuaState *L)
{
    Size sz = m_cobj->GetSize();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &sz);
    obj.Push(L);
    return 1;
}

void LuaWindow::SetSize(int w, int h)
{
    m_cobj->SetSize(w, h);
}

int LuaWindow::GetRect(LuaState *L)
{
    Rect rc = m_cobj->GetRect();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &rc);
    obj.Push(L);
    return 1;
}

void LuaWindow::SetRect(int x, int y, int w, int h)
{
    m_cobj->SetRect(x, y, w, h);
}

int LuaWindow::GetMaxSize(LuaState *L)
{
    Size sz = m_cobj->GetMaxSize();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &sz);
    obj.Push(L);
    return 1;
}

void LuaWindow::SetMaxSize(int w, int h)
{
    m_cobj->SetMaxSize(w, h);
}

int LuaWindow::GetMinSize(LuaState *L)
{
    Size sz = m_cobj->GetMinSize();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &sz);
    obj.Push(L);
    return 1;
}

void LuaWindow::SetMinSize(int w, int h)
{
    m_cobj->SetMinSize(w, h);
}

bool LuaWindow::SetFullScreen(const char* type)
{
    WndFullScreen flag = ParserWndFullScreen(type);
    return m_cobj->SetFullScreen(flag);
}

void LuaWindow::MaxWindow()
{
    m_cobj->MaxWindow();
}

void LuaWindow::MinWindow()
{
    m_cobj->MinWindow();    
}

void LuaWindow::RestoreWindow()
{
    m_cobj->RestoreWindow();
}

void LuaWindow::ShowWindow()
{
    m_cobj->ShowWindow();
}

void LuaWindow::HideWindow()
{
    m_cobj->HideWindow();
}

bool LuaWindow::IsEnabled()
{
    return m_cobj->IsEnabled();
}

void LuaWindow::SetEnabled(bool enabled)
{
    m_cobj->SetEnabled(enabled);
}

void LuaWindow::Close()
{
    m_cobj->Close();
}

void LuaWindow::AskClose()
{
    m_cobj->AskClose();
}

bool LuaWindow::IsOK()
{
    return m_cobj->IsOK();
}

int LuaWindow::GetFont(LuaState *L)
{
    IFont* font = m_cobj->GetFont();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, font);
    obj.Push(L);
    return 1;
}

int LuaWindow::SetFont(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    //cause RegisterObjectFunctor,the first param must be args[2]
    IFont* font = (IFont*) LuaHelperNS::GetCObjectData(args[2]);
    bRet = m_cobj->SetFont(font);
    L->PushBoolean(bRet);
    return 1;
}

int LuaWindow::SetCursor(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    ICursor* cursor = (ICursor*) LuaHelperNS::GetCObjectData(args[2]);
    m_cobj->SetCursor(cursor);
    return 0;
}

int LuaWindow::GetCursor(LuaState *L)
{
    ICursor* cursor = m_cobj->GetCursor();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, cursor);
    obj.Push(L);
    return 1;
}

Uint32 LuaWindow_TimerCallback(TimerID id, Uint32 interval, IWindow* wnd, void *data1, void* data2)
{
    return LuaMgrPtr->DoTimer(wnd, id, interval);
}

//virtual TimerID AddTimer(Uint32 interval, TimerCallback callback, IWindow* wnd);
int LuaWindow::AddTimer(LuaState *L)
{
    LuaStack args(L);
    TimerID id = 0;
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger() && args[3].IsFunction()){
        id = m_cobj->AddTimer(args[2].GetInteger(), LuaWindow_TimerCallback);
        if (id != 0){
            if (!LuaHelperNS::SetTimerToObjs(L, m_cobj, id, args[3])){
                m_cobj->RemoveTimer(id);
                id = 0;
            }
        }
    }
    L->PushInteger(id);
    return 1;
}

int LuaWindow::RemoveTimer(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        TimerID id = args[2].GetInteger();
        m_cobj->RemoveTimer(id);
        LuaHelperNS::RemoveTimerFromObjs(L, m_cobj, id);
    }
    return 0;
}

int LuaWindow::SetParent(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    //cause RegisterObjectFunctor,the first param must be args[2]
    IWindow* wnd = (IWindow*) LuaHelperNS::GetCObjectData(args[2]);
    bRet = m_cobj->SetParent(wnd);
    L->PushBoolean(bRet);
    return 1;
}

int LuaWindow::GetParent(LuaState *L)
{
    IWindow* wnd = m_cobj->GetParent();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
    obj.Push(L);
    return 1;
}

bool LuaWindow::SetOpacity(float opacity)
{
    return m_cobj->SetOpacity(opacity);
}

float LuaWindow::GetOpacity()
{
    return m_cobj->GetOpacity();
}

int LuaWindow::SetFocusCtrl(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    Control* ctrl = (Control*) LuaHelperNS::GetCObjectData(args[2]);
    m_cobj->SetFocusCtrl(ctrl);
    return 0;
}

int LuaWindow::GetFocusCtrl(LuaState *L)
{
    Control* ctrl = m_cobj->GetFocusCtrl();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
    obj.Push(L);
    return 1;
}

void LuaWindow::TabCtrlNext()
{
    return m_cobj->TabCtrlNext();
}

int LuaWindow::SetTabCtrl(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    Control* ctrl = (Control*) LuaHelperNS::GetCObjectData(args[2]);
    m_cobj->SetTabCtrl(ctrl);
    return 0;
}

int LuaWindow::GetTabCtrl(LuaState *L)
{
    Control* ctrl = m_cobj->GetTabCtrl();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
    obj.Push(L);
    return 1;
}

void LuaWindow::Invaldate()
{
    m_cobj->Invaldate();
}

int LuaWindow::GetRoot(LuaState* L)
{
    Container* ctrl = m_cobj->GetRoot();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
    obj.Push(L);
    return 1;
}

int LuaWindow::FindControl(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = m_cobj->FindControl(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaWindow::FindControlByTabID(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        Control* ctrl = m_cobj->FindControlByTabID(args[2].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

bool LuaWindow::LoadLayout(const char* xmlfile)
{
    return m_cobj->LoadLayout(xmlfile);
}

bool LuaWindow::LoadLayoutFromStr(const char* xmlstr)
{
    return m_cobj->LoadLayoutFromStr(xmlstr);
}

int LuaWindow::LoadControl(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = m_cobj->LoadControl(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaWindow::LoadControlFromStr(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = m_cobj->LoadControlFromStr(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaWindow::FindTexture(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        bool autoload = false;
        if (args[3].IsBoolean()) autoload = args[3].GetBoolean();
        ITexture* texture = m_cobj->FindTexture(args[2].GetString(), autoload);
        LuaObject obj = LuaHelperNS::GetFromObjs(L, texture);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaWindow::FindTextureByID(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        bool autoload = false;
        if (args[3].IsBoolean()) autoload = args[3].GetBoolean();
        ITexture* texture = m_cobj->FindTexture(args[2].GetInteger(), autoload);
        LuaObject obj = LuaHelperNS::GetFromObjs(L, texture);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaWindow::FindAnimation(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Animation* animation = m_cobj->FindAnimation(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, animation);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaWindow::FindAnimationByID(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        Animation* animation = m_cobj->FindAnimation(args[2].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, animation);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

//----------------------------------------//

void LuaWindow::Register(LuaState *L)
{
    LuaObject obj = LuaHelperNS::AddToMetas(L, "Window");
    obj.SetObject("__index", obj);
    obj.RegisterDirect("__NewCObj__", &LuaWindow::GetInterface);

    obj.RegisterObjectDirect("SetName", (LuaWindow*)0, &LuaWindow::SetName);
    obj.RegisterObjectDirect("GetName", (LuaWindow*)0, &LuaWindow::GetName);
    obj.RegisterObjectDirect("GetID", (LuaWindow*)0, &LuaWindow::GetID);
    obj.RegisterObjectDirect("GetFlags", (LuaWindow*)0, &LuaWindow::GetFlags);
    obj.RegisterObjectDirect("GetTitle", (LuaWindow*)0, &LuaWindow::GetTitle);
    obj.RegisterObjectDirect("SetTitle", (LuaWindow*)0, &LuaWindow::SetTitle);
    obj.RegisterObjectFunctor("SetIcon", &LuaWindow::SetIcon);
    obj.RegisterObjectFunctor("GetIcon", &LuaWindow::GetIcon);
    obj.RegisterObjectDirect("SetCaptionRect", (LuaWindow*)0, &LuaWindow::SetCaptionRect);
    obj.RegisterObjectFunctor("GetCaptionRect", &LuaWindow::GetCaptionRect);
    obj.RegisterObjectDirect("SetSizeBox", (LuaWindow*)0, &LuaWindow::SetSizeBox);
    obj.RegisterObjectFunctor("GetSizeBox", &LuaWindow::GetSizeBox);
    obj.RegisterObjectDirect("GetWidth", (LuaWindow*)0, &LuaWindow::GetWidth);
    obj.RegisterObjectDirect("GetHeight", (LuaWindow*)0, &LuaWindow::GetHeight);
    obj.RegisterObjectFunctor("GetPos", &LuaWindow::GetPos);
    obj.RegisterObjectDirect("SetPos", (LuaWindow*)0, &LuaWindow::SetPos);
    obj.RegisterObjectFunctor("GetSize", &LuaWindow::GetSize);
    obj.RegisterObjectDirect("SetSize", (LuaWindow*)0, &LuaWindow::SetSize);
    obj.RegisterObjectFunctor("GetRect", &LuaWindow::GetRect);
    obj.RegisterObjectDirect("SetRect", (LuaWindow*)0, &LuaWindow::SetRect);
    obj.RegisterObjectDirect("SetFullScreen", (LuaWindow*)0, &LuaWindow::SetFullScreen);
    obj.RegisterObjectFunctor("GetMaxSize", &LuaWindow::GetMaxSize);
    obj.RegisterObjectDirect("SetMaxSize", (LuaWindow*)0, &LuaWindow::SetMaxSize);
    obj.RegisterObjectFunctor("GetMinSize", &LuaWindow::GetMinSize);
    obj.RegisterObjectDirect("SetMinSize", (LuaWindow*)0, &LuaWindow::SetMinSize);
    obj.RegisterObjectDirect("MaxWindow", (LuaWindow*)0, &LuaWindow::MaxWindow);
    obj.RegisterObjectDirect("MinWindow", (LuaWindow*)0, &LuaWindow::MinWindow);
    obj.RegisterObjectDirect("RestoreWindow", (LuaWindow*)0, &LuaWindow::RestoreWindow);
    obj.RegisterObjectDirect("ShowWindow", (LuaWindow*)0, &LuaWindow::ShowWindow);
    obj.RegisterObjectDirect("HideWindow", (LuaWindow*)0, &LuaWindow::HideWindow);
    obj.RegisterObjectDirect("IsEnabled", (LuaWindow*)0, &LuaWindow::IsEnabled);
    obj.RegisterObjectDirect("SetEnabled", (LuaWindow*)0, &LuaWindow::SetEnabled);
    obj.RegisterObjectDirect("Close", (LuaWindow*)0, &LuaWindow::Close);
    obj.RegisterObjectDirect("AskClose", (LuaWindow*)0, &LuaWindow::AskClose);
    obj.RegisterObjectDirect("IsOK", (LuaWindow*)0, &LuaWindow::IsOK);
    obj.RegisterObjectFunctor("GetFont", &LuaWindow::GetFont);
    obj.RegisterObjectFunctor("SetFont", &LuaWindow::SetFont);
    obj.RegisterObjectFunctor("GetCursor", &LuaWindow::GetCursor);
    obj.RegisterObjectFunctor("SetCursor", &LuaWindow::SetCursor);
    obj.RegisterObjectFunctor("AddTimer", &LuaWindow::AddTimer);
    obj.RegisterObjectFunctor("RemoveTimer", &LuaWindow::RemoveTimer);
    obj.RegisterObjectFunctor("SetParent", &LuaWindow::SetParent);
    obj.RegisterObjectFunctor("GetParent", &LuaWindow::GetParent);
    obj.RegisterObjectDirect("SetOpacity", (LuaWindow*)0, &LuaWindow::SetOpacity);
    obj.RegisterObjectDirect("GetOpacity", (LuaWindow*)0, &LuaWindow::GetOpacity);
    obj.RegisterObjectFunctor("SetFocusCtrl", &LuaWindow::SetFocusCtrl);
    obj.RegisterObjectFunctor("GetFocusCtrl", &LuaWindow::GetFocusCtrl);
    obj.RegisterObjectDirect("TabCtrlNext", (LuaWindow*)0, &LuaWindow::TabCtrlNext);
    obj.RegisterObjectFunctor("SetTabCtrl", &LuaWindow::SetTabCtrl);
    obj.RegisterObjectFunctor("GetTabCtrl", &LuaWindow::GetTabCtrl);
    obj.RegisterObjectDirect("Invaldate", (LuaWindow*)0, &LuaWindow::Invaldate);
    obj.RegisterObjectFunctor("GetRoot", &LuaWindow::GetRoot);
    obj.RegisterObjectFunctor("FindControl", &LuaWindow::FindControl);
    obj.RegisterObjectFunctor("FindControlByTabID", &LuaWindow::FindControlByTabID);
    obj.RegisterObjectDirect("LoadLayout", (LuaWindow*)0, &LuaWindow::LoadLayout);
    obj.RegisterObjectDirect("LoadLayoutFromStr", (LuaWindow*)0, &LuaWindow::LoadLayoutFromStr);
    obj.RegisterObjectFunctor("LoadControl", &LuaWindow::LoadControl);
    obj.RegisterObjectFunctor("LoadControlFromStr", &LuaWindow::LoadControlFromStr);
    obj.RegisterObjectFunctor("FindTexture", &LuaWindow::FindTexture);
    obj.RegisterObjectFunctor("FindTextureByID", &LuaWindow::FindTextureByID);
    obj.RegisterObjectFunctor("FindAnimation", &LuaWindow::FindAnimation);
    obj.RegisterObjectFunctor("FindAnimationByID", &LuaWindow::FindAnimationByID);
}

EDUI_NAMESPACE_END

#endif